package pro.reznick.geoalarm;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.List;
import java.util.ArrayList;

public class AlarmDao
{

    private SQLiteDatabase db;

    public AlarmDao(Context context)
    {
        AlarmDaoHelper helper = new AlarmDaoHelper(context);
        db = helper.getWritableDatabase();
    }

    public List<AlarmData> getAlarms()
    {

        Cursor cursor = null;
        List<AlarmData> ret = new ArrayList<AlarmData>();
        try
        {
            cursor = db.query("alarm", new String[]{"id", "longitude",
                    "latitude", "distanceInMeters", "name", "text", "is_on"},
                    null, null, null, null, "id");
            cursor.moveToFirst();
            while (cursor.isAfterLast() == false)
            {
                ret.add(readAlarmData(cursor));
                cursor.moveToNext();
            }
        }
        finally
        {
            if (cursor != null && !cursor.isClosed())
                cursor.close();
        }
        return ret;

    }

    private AlarmData readAlarmData(Cursor cursor)
    {
        return new AlarmData(cursor.getInt(0), cursor.getDouble(1), cursor
                .getDouble(2), cursor.getInt(3), cursor.getString(4), cursor
                .getString(5), cursor.getInt(6) == 1 ? true : false);
    }

    public void newAlarm(AlarmData alarm)
    {
        ContentValues values = prepareContentValues(alarm);

        long id = db.insert("alarm", null, values);
        alarm.setId((int)id);

    }

    public void delete(AlarmData alarm)
    {
        db.delete("alarm", "id=?",
                new String[]{Long.toString(alarm.getId())});
    }

    public void close()
    {
        db.close();
    }

    public AlarmData get(int alarmId)
    {
        Cursor cursor = null;
        try
        {
            cursor = db.query("alarm", new String[]{"id", "longitude",
                    "latitude", "distanceInMeters", "name", "text", "is_on"},
                    "id= ?", new String[]{Long.toString(alarmId)}, null,
                    null, "id");
            if (cursor.moveToFirst())
                return readAlarmData(cursor);
        }
        finally
        {
            if (cursor != null && !cursor.isClosed())
                cursor.close();
        }
        return null;

    }

    public void updateAlarm(AlarmData alarm)
    {
        ContentValues values = prepareContentValues(alarm);
        db.update("alarm", values, "id = ?", new String[]{Long.toString(alarm.getId())});

    }


    public void setAlarmState(long alarmId, boolean isOn)
    {
        ContentValues values = new ContentValues();
        values.put("is_on", isOn);
        db.update("alarm", values, "id = ?", new String[]{Long.toString(alarmId)});

    }

    private ContentValues prepareContentValues(AlarmData alarm)
    {
        ContentValues values = new ContentValues();
        values.put("longitude", alarm.getLongitude());
        values.put("latitude", alarm.getLatitude());
        values.put("distanceInMeters", alarm.getDistanceInMeters());
        values.put("name", alarm.getName());
        values.put("text", alarm.getText());
        values.put("is_on", alarm.isOn());
        return values;
    }

}
